<?php
    include_once './config.php';
    $user = checkLogin();
    $date = date("Y-m-d",time());
    $query = "select * from whw_user where type = 2 order by updatetime desc";
    $rows = $objDBCD14->query($query);
    $workerList = [];
    if ($rows->num_rows) {
        foreach ($rows as $key => $worker) {
            $workerList[] =  $worker; 
        }  
    }
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>师傅管理</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        body{
           background: #f8f8f8; 
           height: 100%;
        }
        .container{
            margin: 0;
        }
        header{
            display: flex;
            align-items: center;
            background: #e64340;
            padding: 12px;
            color: #fff;
            justify-content: space-between;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 44px;
            box-sizing: border-box;
        }
        header .title{
            text-align: center;
        }
        header .layui-icon{
            font-size: 18px;
        }
        main{
            padding: 44px 0 0;
        }
        .list{
            padding: 10px;
        }
        .list .item{
            background: #fff;
            padding: 15px;
            border-radius: 5px;
            margin: 10px 0;
        }
        .list .item .username{
            font-size: 16px;
        }
        .list .item .username span{
          display: inline-block;
          margin-right: 10px;
        }
    </style>
</head>
<body ontouchstart>
    <div class="container" id="container">
        <header>
            <div>
                <i class="layui-icon layui-icon-left" onclick="history.back()"></i>
            </div>
            <div class="title">师傅管理</div>
            <div class="add-btn">
                <i class="layui-icon layui-icon-add-1"></i>
            </div>
        </header>
        <main>
            <?php if(count($workerList)) { ?>
                <div class="list">
                    <?php for($i = 0;$i < count($workerList);$i++) { ?>
                        <div class="item">
                            <div style="display:flex;justify-content:space-between;align-items:center">
                                <div class="username">
                                   <span><?= $workerList[$i]['username'] ?></span>
                                   <span><?= $workerList[$i]['phone'] ?></span>
                                </div>
                                <div style="color: green">
                                    佣金比例：<?= $workerList[$i]['commission'] ?>%
                                </div>
                            </div>
                            <div style="display:flex;justify-content:space-between;align-items:center;margin-top: 10px;border-top: 1px solid #f7f7f7;padding-top: 15px">
                                <!--<div><?= date("Y-m-d H:i:s",$workerList[$i]['updatetime']) ?></div>-->
                                <?php if($workerList[$i]['can_order']) { ?>
                                    <div style="color: #f17850">
                                        已授权创建订单
                                    </div>
                                <?php } else {?>
                                     <div>
                                        未授权创建订单
                                    </div>
                                <?php } ?>
                                <div>
                                    <button type="button" class="layui-btn layui-btn-sm edit"
                                    data-id="<?= $workerList[$i]['id'] ?>">编辑</button>
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-danger face"
                                    data-id="<?= $workerList[$i]['id'] ?>" data-username="<?= $workerList[$i]['username'] ?>">耗材管理</button>
                                </div>
                            </div>
                        </div>
                    <?php } ?>
                </div>
            <?php } else { ?>
            <img src="./images/data-empty.png" style="width: 200px;height:auto;display:block;margin: 30vh auto" />
            <?php } ?>
        </main>
    </div>
    <!-- 引入 layui.js -->
    <script src="./layui/layui.js"></script>
    <script>
        layui.use(['layer', 'form','code'], function(){
            var $ = layui.$,layer = layui.layer,form = layui.form,code = layui.code;
            // 添加
            $(".add-btn").click(function(){
                layer.open({
                  type: 2, 
                  title: "添加师傅",
                  area: ['90vw','400px'],
                  content: './worker-add.php',
                  shadeClose: false,
                  success: function(layero, index){
                     layer.iframeAuto(index)
                  },
                });
            })
            // 编辑
            $(".edit").click(function(){
                var user_id = $(this).data("id")
                layer.open({
                  type: 2, 
                  title: "编辑师傅",
                  area: ['90vw','400px'],
                  content: './worker-add.php?user_id='+user_id,
                  shadeClose: false,
                  fixed: true,
                  offset: 'auto', // 自动偏移
                  success: function(layero, index){
                    //  layer.iframeAuto(index)
                  },
                });
            })
            // 耗材管理
            $(".face").click(function(){
                var user_id = $(this).data("id")
                var username = $(this).data("username")
                layer.open({
                  type: 2, 
                  title: "<font color='green'>" +username +"</font>的耗材管理",
                  area: ['90vw','80vh'],
                  content: './face.php?user_id='+user_id,
                  shadeClose: false,
                  fixed: true,
                  offset: 'auto', // 自动偏移
                  success: function(layero, index){
                    
                  },
                });
            })
        });
    </script>  
</body>
</html>